package main

import (
	"errors"
	"fmt"
)

type ConnectConfig struct {
	URL, PASSWORD, USERNAME string
}

type Datasource interface {
	GetConnection(config ConnectConfig) (bool, error)
}

type Mysql struct {
}

func (sql Mysql) GetConnection(config ConnectConfig) (result bool, error error) {
	fmt.Println("sql链接成功")
	if config.URL == "" || config.PASSWORD == "" || config.USERNAME == "" {
		return false, errors.New("操作异常")
	}
	fmt.Println("操作成功")
	return true, nil
}

type Oracle struct {
}

func (sql Oracle) GetConnection(config ConnectConfig) (result bool, error error) {
	fmt.Println("sql链接成功")
	if config.URL == "" || config.PASSWORD == "" || config.USERNAME == "" {
		result = false
		error = errors.New("操作异常")
		return
	}
	fmt.Println("操作成功")
	return true, nil
}

func connect() {
	var config = ConnectConfig{
		URL:      "http://localhost:3306",
		PASSWORD: "",
		USERNAME: "root",
	}
	var connect Datasource = Mysql{}
	connection, err := connect.GetConnection(config)
	if err != nil {
		fmt.Println("连接失败==========>", err.Error())
	}
	fmt.Println("连接成功", connection)
}
func main() {
	connect()
}

// 类型断言
func testType(s any) {
	if _, ok := s.(Datasource); ok {
		fmt.Println("Datasource类型")
	}
}
